<template>
  <div class="app-container">
    <el-form :inline="true" :model="video" class="demo-form-inline">
      <el-form-item label="科目">
        <el-input v-model="video.subjectId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="视频汇总名">
        <el-input v-model="video.videoName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="video.videoAuthor" placeholder=""></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onClear">清空</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="linkToAdd">新增视频汇总</el-button>
    <el-button type="danger" v-show="showDelBtn" @click="delVideos"
      >删除</el-button
    >
    <el-table
      v-loading="listLoading"
      :data="videos"
      row-key="id"
      element-loading-text="Loading"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      >

      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <!--el-table-column label="科目" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.subjectId }}
        </template>
      </el-table-column-->
      <el-table-column label="视频汇总名" width="300" align="center">
        <template slot-scope="scope">
          {{ scope.row.videoName }}
        </template>
      </el-table-column>
      <el-table-column label="作者" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.videoAuthor }}
        </template>
      </el-table-column>
      <el-table-column label="价格" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="上传人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.userId }}
        </template>
      </el-table-column>
      <!--el-table-column label="排序号" width="95" align="center">
        <template slot-scope="scope">
          {{ scope.row.orderNum }}
        </template>
      </el-table-column-->
      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>
            {{ scope.row.createTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-view"
            circle
            @click="showVideoInfos(scope.row.id)"
          />
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="linkToEdit(scope.row.id)"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="delVideo(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="视频列表" :visible.sync="dialogVisible">
      <el-table :data="videoInfos" row-key="id" style="width: 100%">
      <el-table-column label="视频名称" prop="videoName"> </el-table-column>
      <el-table-column label="视频作者" prop="videoName"> </el-table-column>
      <el-table-column label="排序" prop="videoName"> </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <nav style="text-align: center">
      <el-pagination
        background
        @current-change="handlePageChange"
        :current-page="pageNo"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </nav>
  </div>
</template>

<script>
import { getVideosApi, delVideoApi, delVideosApi } from "@/api/video";
import { getAllVideoInfosApi } from "@/api/videoInfo";
export default {
  data() {
    return {
      //imgPath:process.env.VUE_APP_BASE_API,
      video: {},
      pageNo: 1,
      pageSize: 10,
      videos: [],
      dialogVisible: false,
      videoInfos: [],
      listLoading: true,
      delVideoArray: [],
      showDelBtn: false,
      total: 0,
    };
  },
  created() {
    this.getVideos(this.pageNo, this.pageSize, this.video);
  },
  methods: {
    handlePageChange(currentPage) {
      this.pageNo = currentPage;
      this.getVideos(this.pageNo, this.pageSize, this.video);
    },
    handleSelectionChange(val) {
      if (val.length > 0) {
        this.showDelBtn = true;
      } else {
        this.showDelBtn = false;
      }
      this.delVideoArray = val;
    },
    delVideos() {
      this.listLoading = true;
      delVideosApi(this.delVideoArray).then((response) => {
        this.getVideos(this.pageNo, this.pageSize);
        this.listLoading = false;
      });
    },
    linkToAdd() {
      this.$router.push("/content/video/addVideo");
    },
    onSearch() {
      this.pageNo = 1;
      this.getVideos(this.pageNo, this.pageSize, this.video);
    },
    onClear() {
      this.video = {};
      this.getVideos(this.pageNo, this.pageSize, this.video);
    },
    getVideos(pageNo, pageSize, video) {
      this.listLoading = true;
      getVideosApi(pageNo, pageSize, this.video).then((response) => {
        this.videos = response.data.list;
        this.pageNo = response.data.pageNum;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    showVideoInfos(id) {
      this.dialogVisible = true;
      getAllVideoInfosApi(id)
        .then(response=>{
          this.videoInfos = response.data
        })
    },
    linkToEdit(id) {
      this.$router.push({ name: "editVideo", params: { id: id } });
    },
    delVideo(id) {
      this.listLoading = true;
      delVideoApi(id).then((response) => {
        this.getVideos(this.pageNo, this.pageSize);
        this.listLoading = false;
      });
    },
  },
};
</script>
